﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WcMatch.Models;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace WcMatch.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        StudentContext _studentContext;
        CompanyContext _companyContext;
        CollegeContext _collegeContext;
        UniversityContext _universityContext;

        public LoginController( StudentContext studentContext,
                                CompanyContext companyContext,
                                CollegeContext collegeContext,
                                UniversityContext universityContext)
        {
            _studentContext = studentContext;
            _companyContext = companyContext;
            _collegeContext = collegeContext;
            _universityContext  = universityContext;
        }

        [HttpPost]
        public IActionResult StuLogin(string uname, string pwd)
        {
            Student student = _studentContext.Students.FirstOrDefault(s => s.UName == uname && s.HashedPwd == pwd);
            if(student == null)
                return NotFound(new ActionStatus(-1, "用户不存在", string.Empty));
            return Ok(new ActionStatus(200, "登录成功", student.Uid));
        }

        [HttpPost]
        public IActionResult ComLogin(string uid, string pwd)
        {
            var company = _companyContext.Companies.FirstOrDefault(s => s.UName == uid && s.Pwd == pwd);
            if (company != null)
            {
                //company.Token = Guid.NewGuid().ToString();
                //_companyContext.SaveChanges();
                return Ok(new ActionStatus(200, "登录成功", company.Uid));
            }
            return NotFound(new ActionStatus(-1, "用户不存在", string.Empty));

        }

        [HttpPost]
        public IActionResult CollLogin(string uid, string pwd)
        {
            var college = _collegeContext.Colleges.FirstOrDefault(s => s.UName == uid && s.Pwd == pwd);
            if (college == null)
            {
                //college.Token = Guid.NewGuid().ToString();
                return NotFound(new ActionStatus(-1, "用户不存在", string.Empty));
            }
            return Ok(new ActionStatus(200, "登录成功", college.Uid));
        }

        //[HttpPost]
        //public IActionResult UniLogin(string uid, string pwd)
        //{
        //    var university = _universityContext.Universities.FirstOrDefault(s => s.UName == uid && s.HashedPwd == pwd);
        //    if (university == null)
        //    {
        //        //university.Token = Guid.NewGuid().ToString();
        //        return NotFound(new ActionStatus(-1, "用户不存在", string.Empty));
        //    }
        //    return Ok(new ActionStatus(200, "登录成功", university.Uid));
        //}
    }
}
